package com.deer.wms.instock.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deer.wms.ASN.manage.constant.AsnManageConstant;
import com.deer.wms.ASN.manage.dao.AsnMasterMapper;
import com.deer.wms.ASN.manage.model.asn.AsnDetail;
import com.deer.wms.ASN.manage.model.asn.AsnMaster;
import com.deer.wms.ASN.manage.model.in.AcceptInsert;
import com.deer.wms.ASN.manage.model.in.AcceptRecord;
import com.deer.wms.ASN.manage.service.AcceptRecordService;
import com.deer.wms.ASN.manage.service.AsnDetailService;
import com.deer.wms.base.system.service.CellInfoService;
import com.deer.wms.instock.constant.InStockConstant;
import com.deer.wms.instock.model.transfer.TransferBill;
import com.deer.wms.instock.service.TransferBillService;
import com.deer.wms.instock.service.WaveService;
import com.deer.wms.instock.service.enter.StrategyEnterService;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.inventory.model.Inventory.Inventory;
import com.deer.wms.inventory.service.InventoryService;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.ware.task.model.Wave.WaveDetail;
import com.deer.wms.ware.task.model.pushErp.beasAssist.BaseAssist;
import com.deer.wms.ware.task.service.SoMasterService;
import com.deer.wms.ware.task.service.WaveDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

/**
 * @Author: xu
 * @Date: 2022/06/23/11:27
 * @Description:
 */
@Service
public class WaveServiceImpl implements WaveService {
    @Autowired
    private WaveDetailService waveDetailService;
    @Autowired
    private InventoryService inventoryService;
    @Autowired
    private TransferBillService transferBillService;
    @Autowired
    private AcceptRecordService acceptRecordService;
    @Autowired
    private AsnDetailService asnDetailService;
    @Autowired
    private CellInfoService cellInfoService;
    @Autowired
    private AsnMasterMapper asnMasterMapper;
    @Autowired
    private StrategyEnterService strategyEnterService;
    @Autowired
    private SoMasterService soMasterService;




    @Override
    public List<String> findCode(List<String> waveDetailIds, Integer asnStockId) {
        List<WaveDetail> waveDetails = waveDetailService.listByIds(waveDetailIds);
        if (ObjectUtil.isNotEmpty(waveDetails)) {
            return null;
        }

        List<String> itemCodes = waveDetails.stream().map(WaveDetail::getItemCode).collect(Collectors.toList());
        LambdaQueryWrapper<Inventory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Inventory::getCellCode,28500);//验收区
        queryWrapper.in(Inventory::getItemCode,itemCodes.size() == 0?"":itemCodes);
        queryWrapper.eq(Inventory::getAsnStockId,471713);
        List<Inventory> inventories = inventoryService.list(queryWrapper);
        List<String> collect = inventories.stream().map(Inventory::getBoxCode).collect(Collectors.toList());
        return collect;

    }





    public void inputBank( String cellCode, CurrentUser currentUser,List<Inventory> inventories) {

        //Step2.准备数据
        List<TransferBill> transferBills = new ArrayList<>();
        QueryWrapper<TransferBill> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("to_box_code", inventories.stream().map(Inventory::getBoxCode).collect(Collectors.toSet()).size() == 0?"":inventories.stream().map(Inventory::getBoxCode).collect(Collectors.toSet()));
        //转移单
        List<TransferBill> transferBillList = transferBillService.list(queryWrapper);
        //根据编号分组
        Set<String> asnDetailNos = transferBillList.stream().map(TransferBill::getUda2).collect(Collectors.toSet());
        List<AsnDetail> asnDetails = asnDetailService.list(new QueryWrapper<AsnDetail>().in("detail_no", asnDetailNos.size() == 0?"":asnDetailNos));
        //根据容器分组
        Map<String, AsnDetail> asnDetailMap = asnDetails.stream().collect(Collectors.toMap(AsnDetail::getDetailNo, asnDetail -> asnDetail, (oldmenu, newMenu) -> newMenu));
        //根据详情单号分组
        Map<String, TransferBill> tranByMap = transferBillList.stream().collect(Collectors.toMap(TransferBill::getToBoxCode, item -> item, (oldMenu, newMenu) -> newMenu));

        List<AsnDetail> asnDetais = new ArrayList<>();

        inventories.forEach(item -> {
            Integer erpAreaId = cellInfoService.findbyCellCode(cellCode);
            TransferBill transferBill1 = tranByMap.get(item.getBoxCode());
            AsnDetail asnDetail = asnDetailMap.get(transferBill1.getUda2());
            asnDetail.setQty(transferBill1.getQty().doubleValue());
            asnDetail.setStockId(erpAreaId);
            asnDetail.setBatchName(item.getBatchName());
            asnDetais.add(asnDetail);
//            String cellCode = item.getCellCode();
            TransferBill transferBill = new TransferBill();
            //仓库
            transferBill.setWareId(285);
            //转移单类型
            transferBill.setBillType(InStockConstant.TRANS_TYPE_UPPER);
            //来源库位 收货区库位
            Inventory boxIvt = inventoryService.getOneByField("box_code", item.getBoxCode());
            String batchName = boxIvt.getBatchName();
            transferBill.setFromCellCode(boxIvt.getCellCode());
            //目标库位
            transferBill.setToCellCode(cellCode);
            //来源容器
            transferBill.setFromBoxCode(item.getBoxCode());
            transferBill.setItemCode(boxIvt.getItemCode());
            transferBill.setBatchName(boxIvt.getBatchName());
            transferBill.setPackDetailId(boxIvt.getPackDetailId());
            transferBill.setQty(new BigDecimal(boxIvt.getQuantity()));
            //标记冻结解冻标识
            if (boxIvt.getFreezeQuantity() > 0) {
                //说明是待检的物料
                transferBill.setFreezeFlag(AsnManageConstant.ACCEPT_QC_STATE_TODO);
            }
            //确认人
            transferBill.setConfirmUserId(currentUser.getUserId());
            transferBill.setConfirmUserName(currentUser.getUserName());
            //复制,供应商,id,供应商名称,部门id,部门名称
            transferBills.add(transferBill);
        });
        //Step3.保存 生成库存事务
        transferBillService.saveAndConfirmTransferBill(transferBills);

        //上架回传erp
        List<AcceptRecord> billNos = acceptRecordService.findBillNo(transferBills.get(0).getFromBoxCode());
        AsnMaster asnMaster = asnMasterMapper.selectOne(new QueryWrapper<AsnMaster>().eq("bill_no", billNos.get(0).getAsnBillNo()));
        BaseAssist enterAssist = new BaseAssist();
        enterAssist.setFromBoxCode(transferBills.get(0).getFromBoxCode());
        enterAssist.setCellCode(cellCode);
        strategyEnterService.resolveAsn(asnMaster, asnDetails);
    }
}
